package com.nuotu.datawall.clouddata.layoutData;

import android.content.Context;
import android.widget.GridView;

import com.google.gson.reflect.TypeToken;
import com.nuotu.datawall.clouddata.R;
import com.nuotu.datawall.clouddata.adapter.BorrowCountadapter;
import com.nuotu.datawall.clouddata.adapter.Grid_jcsj_adapter;
import com.nuotu.datawall.clouddata.been.BasicData;
import com.nuotu.datawall.clouddata.been.BorrowCount;
import com.nuotu.datawall.clouddata.been.ReaderCount;
import com.nuotu.datawall.clouddata.been.RtInfo;
import com.nuotu.datawall.clouddata.control.Token;
import com.nuotu.datawall.clouddata.util.JsonUtil;
import com.nuotu.datawall.clouddata.util.ServerConfig;
import com.nuotu.datawall.clouddata.util.Tool;

import org.xutils.common.Callback;
import org.xutils.common.util.DensityUtil;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;


public class BorrowCountLayout {
    private static volatile BorrowCountLayout instance;
    private static Context mContext;
    private static GridView gv_borrowCount;
    private ArrayList<BorrowCount> borrowCountArrayList;
    private static BorrowCountadapter adapter;
    private int tempSize=0;
    public static BorrowCountLayout getInstance(Context context) {
        if (instance == null) {
            synchronized (BorrowCountLayout.class) {
                if (instance == null) {
                    mContext=context;
                    instance = new BorrowCountLayout();
                }
            }
        }
        return  instance;
    }
    public BorrowCountLayout init(GridView gridView){
        gv_borrowCount=gridView;
        borrowCountArrayList =new ArrayList<BorrowCount>();
        adapter=new BorrowCountadapter(mContext, R.layout.borrowcount_item,borrowCountArrayList);
        gv_borrowCount.setAdapter(adapter);
        gv_borrowCount.setNumColumns(6);
        return instance;
    }
    public void refreshData(){
     RequestParams params = Tool.getRequestParams(mContext, ServerConfig.POST_BORROWCOUNT_6_URL_KEY);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                RtInfo rtInfo= JsonUtil.fromJson(result, RtInfo.class);
                List<BorrowCount> readerCountl=JsonUtil.fromArrayJson(JsonUtil.toJson(rtInfo.getData()),new TypeToken<List<BorrowCount>>(){}.getType());
                borrowCountArrayList.clear();
                borrowCountArrayList.addAll(readerCountl);
                adapter.notifyDataSetChanged();
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Token.getInstance(mContext).refreshToken(ex.toString());
            }
            @Override
            public void onCancelled(CancelledException cex) {
            }
            @Override
            public void onFinished() {

            }
        });
    }
}
